/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IMAGE_KNIFE_C_KEKE_GPU_TRANSFORM_H
#define IMAGE_KNIFE_C_KEKE_GPU_TRANSFORM_H

#include <GLES3/gl3.h>

namespace ImageKnifePro {
class GPUTransform {
public:
    bool InitGL();
    void DeleteGL();
    void Rendering(uint8_t *buffer, uint32_t width, uint32_t height, uint32_t format = GL_RGBA);
protected:
    virtual const char *GetFragmentShaderString() const = 0;
    virtual const char *GetVertexShaderString() const;
    /**
     * @brief 设置片段着色器中uniform变量的值，图形变换期间只执行一次
     */
    virtual void UniformValue()
    {
    }

    /**
     * @brief 设置片段着色器中与图片宽高相关的值，每帧图片变换时都会进行
     */
    virtual void UniformValueWithImageSize(const uint32_t &width, const uint32_t &height)
    {
    }

    GLuint GetProgramObject() const;
private:
    GLuint programObj_ = GL_NONE;
    GLuint fboId_ = GL_NONE;
    GLuint vboIds_[3] = {GL_NONE};
    GLuint vaoIds_[1] = {GL_NONE};

    // 图片纹理
    GLuint imageTextureId_ = GL_NONE;
    // 帧缓冲纹理，图片输出
    GLuint fboTextureId_ = GL_NONE;

    void TextureInit();
    void BufferInit();
    void CreateProgram();
    GLuint CreateShader(GLenum shaderType, const char *pSource);
    void SetImageData(uint8_t *buffer, const uint32_t &width, const uint32_t &height, uint32_t format);
};

}

#endif //IMAGE_KNIFE_C_KEKE_GPU_TRANSFORM_H
